package com.jozen.modelbase.netwoek.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

    public static String getDate(Date date) {
        SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf1.format(date);
    }

    public static String getDate(Date date, String format) {
        SimpleDateFormat sf1 = new SimpleDateFormat(format);
        return sf1.format(date);
    }

    //time的单位：毫秒
    public static String longToString(long time, String format) {
        if (time < 9999999999L) {
            time = time * 1000;
        }
        SimpleDateFormat sf1 = new SimpleDateFormat(format);
        return sf1.format(time);
    }

    public static long stringToLong(String time) {
        SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sf1.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //结束时间，自动补全23：59：59
    public static long stringToLong3(String time) {
        SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sf1.parse(time);
            return date.getTime() + (59 + 59 * 60 + 23 * 3600) * 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        String week = null;
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
        switch (weekDay) {
            case 1:
                week = "星期日";
                break;
            case 2:
                week = "星期一";
                break;
            case 3:
                week = "星期二";
                break;
            case 4:
                week = "星期三";
                break;
            case 5:
                week = "星期四";
                break;
            case 6:
                week = "星期五";
                break;
            case 7:
                week = "星期六";
                break;
        }
        return week;
    }

    //获取当前日期的下next天
    public static String getNextDay(String strDate, int nextDay) {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = dft.parse(strDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, nextDay);
            date = calendar.getTime();
            String next = dft.format(date);
            return next;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取当前日期的下next天
    public static String getNextDay(Date date, int nextDay, String format) {
        DateFormat dft = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, nextDay);
        date = calendar.getTime();
        String next = dft.format(date);
        return next;
    }

    //获取当前时间的小时
    public static int getCurrentHour() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    //获取当前年
    public static int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    //获取当前月
    public static int getCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    //获取当前日
    public static int getCurrentDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }

    //对时间进行补零
    public static String addZero(int time) {
        if (time < 10) {
            return "0" + time + ":00";
        }
        return time + ":00";
    }

    // 获得某天最大时间23:59:59
    public static Date getEndOfDay(int hour, int minute, int second, int addDay, int... args) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (addDay != 0) {
            calendar.add(Calendar.DATE, addDay);
        }
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        if (args.length == 1) {
            calendar.set(Calendar.MILLISECOND, args[0]);
        }
        return calendar.getTime();
    }

}
